package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.IotEdgeEnd;
import com.ruoyi.system.domain.IotManageEnd;
import com.ruoyi.system.domain.vo.IotEdgeEndVo;
import com.ruoyi.system.domain.vo.IotManageEndVo;
import com.ruoyi.system.domain.vo.IotPassthroughParams;
import com.ruoyi.system.domain.vo.IotPassthroughVo;
import com.ruoyi.system.mapper.IotEdgeEndMapper;
import com.ruoyi.system.mapper.IotManageEndMapper;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.IotPassthroughMapper;
import com.ruoyi.system.domain.IotPassthrough;
import com.ruoyi.system.service.IIotPassthroughService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 【请填写功能名称】Service业务层处理
 * 公司名称：未可自动化 <br/>
 * 作者：weikeauto <br/>
 */
@Service
public class IotPassthroughServiceImpl implements IIotPassthroughService {
    @Autowired
    private IotPassthroughMapper iotPassthroughMapper;

    @Autowired
    private IotEdgeEndMapper iotEdgeEndMapper;

    @Autowired
    private IotManageEndMapper iotManageEndMapper;


    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public IotPassthrough selectIotPassthroughById(Long id) {
        return iotPassthroughMapper.selectIotPassthroughById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param iotPassthrough 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<IotPassthrough> selectIotPassthroughList(IotPassthrough iotPassthrough) {
        return iotPassthroughMapper.selectIotPassthroughList(iotPassthrough);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param iotPassthroughParams 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertIotPassthrough(IotPassthroughParams iotPassthroughParams) {

        if (iotPassthroughParams.getIotManageEndIdList().size() > 5) {
            throw new RuntimeException("管理端不能超过五个网关");
        }

        IotPassthrough iotPassthrough = new IotPassthrough();
        iotPassthrough.setCreateTime(DateUtils.getNowDate());
        iotPassthrough.setUpdateTime(DateUtils.getNowDate());
        iotPassthrough.setDeptId(iotPassthroughParams.getDeptId());
        iotPassthrough.setGroupName(iotPassthroughParams.getGroupName());
        iotPassthroughMapper.insertIotPassthrough(iotPassthrough);

        for (Long iotEdgeEndWayId : iotPassthroughParams.getIotEdgeEndIdList()) {
            IotEdgeEnd iotEdgeEnd = new IotEdgeEnd();
            iotEdgeEnd.setCreateTime(DateUtils.getNowDate());
            iotEdgeEnd.setPassthroughId(iotPassthrough.getId());
            iotEdgeEnd.setGatewayId(iotEdgeEndWayId);
            iotEdgeEndMapper.insertIotEdgeEnd(iotEdgeEnd);
        }

        for (Long iotManageEndWayId : iotPassthroughParams.getIotManageEndIdList()) {
            IotManageEnd iotManageEnd=new IotManageEnd();
            iotManageEnd.setCreateTime(DateUtils.getNowDate());
            iotManageEnd.setPassthroughId(iotPassthrough.getId());
            iotManageEnd.setGatewayId(iotManageEndWayId);
            iotManageEndMapper.insertIotManageEnd(iotManageEnd);
        }
        return 1;
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param iotPassthrough 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateIotPassthrough(IotPassthrough iotPassthrough) {
        iotPassthrough.setUpdateTime(DateUtils.getNowDate());
        return iotPassthroughMapper.updateIotPassthrough(iotPassthrough);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteIotPassthroughByIds(Long[] ids) {
        iotPassthroughMapper.deleteIotPassthroughByIds(ids);
        iotPassthroughMapper.deleteIotEdgeEndByThroughIds(ids);
        iotPassthroughMapper.deleteIotManageEndByThroughIds(ids);
        return 1;
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteIotPassthroughById(Long id) {
        return iotPassthroughMapper.deleteIotPassthroughById(id);
    }

    @Override
    @DataScope(deptAlias = "sd")
    public List<IotPassthroughVo> selectNewIotPassthroughList(IotPassthrough iotPassthrough) {
        return iotPassthroughMapper.selectNewIotPassthroughList(iotPassthrough);
    }

    @Override
    public int changeStatus(IotPassthrough iotPassthrough) {
        return iotPassthroughMapper.updateIotPassthrough(iotPassthrough);
    }

    @Override
    public IotPassthroughVo selectIotPassthroughDetailById(Long id) {
        IotEdgeEnd iotEdgeEnd = new IotEdgeEnd();
        iotEdgeEnd.setPassthroughId(id);
        List<IotEdgeEndVo> iotEdgeEndList = iotEdgeEndMapper.selectAllStatusGatewayByPassthrough(id);
        IotManageEnd iotManageEnd = new IotManageEnd();
        iotManageEnd.setPassthroughId(id);
        List<IotManageEndVo> iotManageEndList = iotManageEndMapper.selectAllStatusGatewayByPassthrough(id);
        IotPassthroughVo iotPassthroughVo = new IotPassthroughVo();
        iotPassthroughVo.setIotEdgeEndList(iotEdgeEndList);
        iotPassthroughVo.setIotManageEndList(iotManageEndList);
        return iotPassthroughVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNewIotPassthrough(IotPassthroughParams iotPassthroughParams) {
        Long [] ids = {iotPassthroughParams.getId()};
        iotPassthroughMapper.deleteIotPassthroughByIds(ids);
        iotPassthroughMapper.deleteIotEdgeEndByThroughIds(ids);
        iotPassthroughMapper.deleteIotManageEndByThroughIds(ids);

        if (iotPassthroughParams.getIotManageEndIdList().size() > 5) {
            throw new RuntimeException("管理端不能超过五个网关");
        }

        IotPassthrough iotPassthrough = new IotPassthrough();
        iotPassthrough.setCreateTime(DateUtils.getNowDate());
        iotPassthrough.setUpdateTime(DateUtils.getNowDate());
        iotPassthrough.setDeptId(iotPassthroughParams.getDeptId());
        iotPassthrough.setGroupName(iotPassthroughParams.getGroupName());
        iotPassthroughMapper.insertIotPassthrough(iotPassthrough);

        for (Long iotEdgeEndWayId : iotPassthroughParams.getIotEdgeEndIdList()) {
            IotEdgeEnd iotEdgeEnd = new IotEdgeEnd();
            iotEdgeEnd.setCreateTime(DateUtils.getNowDate());
            iotEdgeEnd.setPassthroughId(iotPassthrough.getId());
            iotEdgeEnd.setGatewayId(iotEdgeEndWayId);
            iotEdgeEndMapper.insertIotEdgeEnd(iotEdgeEnd);
        }

        for (Long iotManageEndWayId : iotPassthroughParams.getIotManageEndIdList()) {
            IotManageEnd iotManageEnd=new IotManageEnd();
            iotManageEnd.setCreateTime(DateUtils.getNowDate());
            iotManageEnd.setPassthroughId(iotPassthrough.getId());
            iotManageEnd.setGatewayId(iotManageEndWayId);
            iotManageEndMapper.insertIotManageEnd(iotManageEnd);
        }
        return 1;
    }
}
